﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mIE.Common
{
    public class SaveFeat_Table
    {
        public static DataTable GetDataTable_Feat(DataTable dtPartTemp,DataTable dtFeature_Values,DataTable dtFeature_Def, string StrDWG_No )
        {
            DataTable dtTempList = Common.GetTable.GetTable_Feature();
            for (int i = 0; i < dtPartTemp.Rows.Count; i++)
            {
                for (int s = 0; s < dtFeature_Values.Rows.Count; s++)
                {
                    //单独计算D34工时
                    if (dtFeature_Values.Rows[s]["PROCESS_CODE"].ToString() == "D34")
                    {

                        DataRow dr = dtTempList.NewRow();
                        dr["DWG_NO"] = StrDWG_No;
                        dr["PART_NO"] = dtPartTemp.Rows[i]["PART_NO"].ToString();
                        dr["PROCESS_CODE"] = dtFeature_Values.Rows[s]["PROCESS_CODE"].ToString();
                        dr["FEATURE_NAME"] = dtFeature_Values.Rows[s]["FEATURE_NAME"].ToString();
                        dr["FEAT_VALUE"] = dtFeature_Values.Rows[s]["FEAT_VALUE"].ToString().Length > 0 ? dtFeature_Values.Rows[s]["FEAT_VALUE"].ToString() : "";
                        if (dtFeature_Values.Rows[s]["FEATURE_NAME"].ToString() == "D34_部件类型")
                        {
                            //得到宽放系数
                            DataTable dtFactor = Common.GetCommon.GetWorkCenter_AllowanceFactor();
                            //得到该号码【D34】尺寸参数
                            DataTable dtSize = Common.GetCommon.GetSizeParmList(StrDWG_No, dtPartTemp.Rows[i]["PART_NO"].ToString());
                            //得到该号码单位工时和附加工时
                            DataTable dtWorkHours = Common.GetCommon.GetFeature_Definatian();
                            if (dtSize.Rows.Count <= 0)
                                dr["LABOR_VALUE"] = "没有维护尺寸参数";
                            else if (dtWorkHours.Rows.Count <= 0)
                                dr["LABOR_VALUE"] = "没有维护特征定义表";
                            else
                            {
                                double TD34 = Math.Ceiling(Calculate.LGC_VAL_D34.GetD34(dtFeature_Values.Select("PROCESS_CODE='D34'"),
                               dtWorkHours.Select(),
                                    dtSize.Select())
                                 //   * (dtFactor.Select("PROCESS_CODE='D34'").Length <= 0 ? 1 :
                                 //1 + Convert.ToDouble(dtFactor.Select(("PROCESS_CODE='D34'"))[0].ItemArray[1].ToString()
                                 //   ))
                                    );
                                dr["LABOR_VALUE"] = TD34;
                            }
                        }
                        else
                            dr["LABOR_VALUE"] = 0;
                        dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.AccountName;
                        dtTempList.Rows.Add(dr);
                    }
                    else
                    {
                        //生成实际数据表
                        DataRow dr = dtTempList.NewRow();
                        dr["DWG_NO"] = StrDWG_No;
                        dr["PART_NO"] = dtPartTemp.Rows[i]["PART_NO"].ToString();
                        dr["PROCESS_CODE"] = dtFeature_Values.Rows[s]["PROCESS_CODE"].ToString();
                        dr["FEATURE_NAME"] = dtFeature_Values.Rows[s]["FEATURE_NAME"].ToString();
                        dr["FEAT_VALUE"] = dtFeature_Values.Rows[s]["FEAT_VALUE"].ToString().Length > 0 ? dtFeature_Values.Rows[s]["FEAT_VALUE"].ToString() : "";
                        //dr["LABOR_VALUE"] = dtFeature_Values.Rows[s]["LABOR_VALUE"].ToString().Length > 0 ? dtFeature_Values.Rows[s]["LABOR_VALUE"].ToString() : "0";
                        //因存在每个部件号可能有不同的工时，故需要重新计算
                        dr["LABOR_VALUE"] =
                           // Calculate.LGC_TIM_D42_SAW.LGC_TIM_D42_SAW_VALUES(dtFeature_Values) == true ? 0 :
                            Calculate.Again_Cal_WorkHours.Get_Cal_WorkHours(dtFeature_Values, dtFeature_Def, StrDWG_No, dtPartTemp.Rows[i]["PART_NO"].ToString(),
                            dtFeature_Values.Rows[s]["FEATURE_NAME"].ToString(),
                            dtFeature_Values.Rows[s]["PROCESS_CODE"].ToString(),
                            dtFeature_Values.Rows[s]["FEAT_VALUE"].ToString().Length > 0 ? dtFeature_Values.Rows[s]["FEAT_VALUE"].ToString() : "");
                        dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.AccountName; ;
                        dtTempList.Rows.Add(dr);
                    }
                }

            }
            return dtTempList;
        }
    }
}
